package com.lianjia.sh.tools.util;
import freemarker.template.TemplateException;
import org.springframework.util.PropertyPlaceholderHelper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 模版工具类：规换字符串
 * Created by yangsp1 on 16/6/25.
 */
public class TemplateUtil {
    static Properties properties = new Properties();
    static PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${","}");

    public static String parseTemplate(Map<String,Object> datas,String templateContent){
        properties.clear();
        properties.putAll(datas);
        return propertyPlaceholderHelper.replacePlaceholders(templateContent,properties);
    }

    public static void main(String args[]) throws IOException, TemplateException, NoSuchMethodException {
        Map<String,Object> params = new HashMap<>();
        params.put("name","yangsp2");
        params.put("yangsp2.gender","男");
        String template = "欢迎${name}(${${name}.gender})来到东莫村";

        String rs2 = parseTemplate(params,template);
        System.out.println(rs2);
    }
}
